#pragma once
#include"inc/cppjieba/Jieba.hpp"
#include"common.hpp"
#include<boost/algorithm/string.hpp>

const char* DICT_PATH = "dict/jieba.dict.utf8";
const char* HMM_PATH = "dict/hmm_model.utf8";
const char* USER_DICT_PATH = "dict/user.dict.utf8";
const char* IDF_PATH = "dict/idf.utf8";
const char* STOP_WORD_PATH = "dict/stop_words.utf8";

namespace Tool{

    class ReadHtml{
        public:
            static bool Readfile(const std::string& path,std::string * out){
                std::ifstream ifs(path,std::ios::in | std::ios::binary);
                if(!ifs.is_open()) return false;
                std::stringstream ss;
                ss<<ifs.rdbuf();

                *out += ss.str();
                // while(getline(ss,*out)){}
                return true;
            }
    };
    
    class BoostSplit{
        public:
            static bool Splithtml(const std::string& line,std::vector<std::string>* onethml,const std::string& sep){
                // std::string cpsep(1,sep);
                boost::split(*onethml,line,boost::is_any_of(sep.c_str()),boost::token_compress_on);
                return true;           
            }
    };

    class Jiebasplit{
        private:

        public:
            static bool Splitkeyword(const std::string& words,std::vector<std::string>* cutwords){
                cutwords->clear();
                static cppjieba::Jieba jieba(DICT_PATH, HMM_PATH, USER_DICT_PATH, IDF_PATH, STOP_WORD_PATH);
                jieba.CutForSearch(words,*cutwords);
                return true;
            }

    };

    class Excerpt{
        public:
            static std::string Getexcerpt(std::string content,std::string keyword){
            #define Cutlength 50
                int start = 0;
                int end = content.size();
                boost::to_lower(content);
                size_t pos = content.find(keyword);
                if(pos == std::string::npos) {
                    std::cerr<<"Getexcerpt false "<<std::endl;;
                    return "Getexcerpt false ";
                }
                if(pos > start + Cutlength) start = pos - Cutlength;
                if(pos + Cutlength < end) end = pos + Cutlength;
                return content.substr(start,end-start);
            }
    };

}